package cn.ym.mall.dao.impl;

import cn.ym.mall.dao.ActiveJoinDao;
import cn.ym.mall.dao.ActiveTypeDao;
import cn.ym.mall.entity.ActiveJoin;
import cn.ym.mall.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 漆家乐
 * 活动参加实现接口类
 */
public class ActiveJoinDaoImpl implements ActiveJoinDao {
    QueryRunner queryRunner = new QueryRunner();
    /**
     * 商家参加活动
     *
     * @param activeJoin
     * @return
     * @throws SQLException
     */
    @Override
    public long add(ActiveJoin activeJoin) throws SQLException {
        long num = 0;
        try{
            String sql = "insert into ym_activeJoin(sid,acjstate) value(?,?);";
            num = queryRunner.update(DruidUtils.getConnection(),sql,activeJoin);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 查询所有商家（活动）
     *
     * @return
     * @throws SQLException
     */
    @Override
    public List<ActiveJoin> getAll() throws SQLException {
        List<ActiveJoin> activeJoinList = new ArrayList<ActiveJoin>();
        try{
            String sql = "select * from ym_activeJoin;";
            activeJoinList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<ActiveJoin>(ActiveJoin.class));
        }catch (Exception e){
            e.printStackTrace();
        }
        return activeJoinList;
    }

    /**
     * 根据id查询商家是否参加活动
     *
     * @param aid
     * @return
     * @throws SQLException
     */
    @Override
    public ActiveJoin getActiveJoinByAid(Integer aid) throws SQLException {
        ActiveJoin activeJoin = null;
        try{
            String sql = "select * from ym_activeJoin where aid = ?;";
            activeJoin = queryRunner.query(sql,new BeanHandler<ActiveJoin>(ActiveJoin.class),aid);
        }catch (Exception e){
            e.printStackTrace();
        }
        return activeJoin;
    }

    /**
     * 查询参加活动的商家
     *
     * @param acjstate
     * @return
     * @throws SQLException
     */
    @Override
    public List<ActiveJoin> getAllJoin(Integer acjstate) throws SQLException {
        List<ActiveJoin> activeJoinList = new ArrayList<ActiveJoin>();
        try{
            String sql = "select * from ym_activeJoin where acjstate=?;";
            activeJoinList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<ActiveJoin>(ActiveJoin.class),acjstate);
        }catch (Exception e){
            e.printStackTrace();
        }
        return activeJoinList;
    }

    /**
     * 分页查询商家（活动）
     *
     * @param pageNo
     * @param pageSize
     * @return
     * @throws SQLException
     */
    @Override
    public List<ActiveJoin> getAllPage(Integer pageNo, Integer pageSize) throws SQLException {
        List<ActiveJoin> activeJoinList = new ArrayList<ActiveJoin>();
        try{
            String sql = "select * from ym_activeJoin order by aid desc limit ?,?;";
            activeJoinList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<ActiveJoin>(ActiveJoin.class),(pageNo-1)*pageSize,pageSize);
        }catch (Exception e){
            e.printStackTrace();
        }
        return activeJoinList;
    }

    /**
     * 分页查询参加活动的商家
     *
     * @param acjstate
     * @param pageNo
     * @param pageSize
     * @return
     * @throws SQLException
     */
    @Override
    public List<ActiveJoin> getAllJoinPage(Integer acjstate, Integer pageNo, Integer pageSize) throws SQLException {
        List<ActiveJoin> activeJoinList = new ArrayList<ActiveJoin>();
        try{
            String sql = "select * from ym_activeJoin where acjstate=? order by aid desc limit ?,?;";
            activeJoinList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<ActiveJoin>(ActiveJoin.class),acjstate,(pageNo-1)*pageSize,pageSize);
        }catch (Exception e){
            e.printStackTrace();
        }
        return activeJoinList;
    }

    /**
     * 查询所有商家（活动）总数
     *
     * @return
     * @throws SQLException
     */
    @Override
    public long getActiveJoinsCount() throws SQLException {
        long count = 0;
        try {
            String sql = "select count(0) as count from ym_activeJoin;";
            count = (queryRunner.query(DruidUtils.getConnection(),sql,new ScalarHandler<Long>()));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 查询参加活动的商家数量
     *
     * @param acjstate
     * @return
     * @throws SQLException
     */
    @Override
    public long getActiveJoinsCountByAid(Integer acjstate) throws SQLException {
        long count = 0;
        try {
            String sql = "select count(0) as count from ym_activeJoin where acjstate = ?;";
            count = (queryRunner.query(DruidUtils.getConnection(),sql,new ScalarHandler<Long>(),acjstate));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }
}
